﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Ebiz.libs;
using System.Web.Services;
using System.Web.Script.Services;

namespace Ebiz
{
    public partial class SystemPreference : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string dbName = DBUtilities.GetDBName("devCon");
            if (!IsPostBack) {
                Dictionary<string, object> ViewData = LoadData();

                Dictionary<string, object> EmailConfig = (Dictionary<string, object>)ViewData["Email"];
                Dictionary<string, object> SysConfig = (Dictionary<string, object>)ViewData["System"];
                cbLogMode.Value = SysConfig["LoginMode"].ToString();
                txtSvrName.Value = SysConfig["ServerAddress"].ToString();
                txtSvrPort.Value = SysConfig["ServerPort"].ToString();
                txtEmailFrom.Value = EmailConfig["FromEmail"].ToString();
                txtEmailName.Value = EmailConfig["FromName"].ToString();
                txtSMTPHost.Value = EmailConfig["Host"].ToString();
                txtSMTPPort.Value = EmailConfig["Port"].ToString();
                txtSMTPUname.Value = EmailConfig["UserName"].ToString();
                txtSMTPPwd.Value = EmailConfig["Password"].ToString();
                cbSMTPCred.Value = EmailConfig["DefaultCredentials"].ToString();
                cbSMTPSSL.Value = EmailConfig["EnableSSL"].ToString();

            } else if (IsPostBack) {
                string echoSuccess = "alert('Update Success');window.location='SystemPreference.aspx';";
                string echoFail = "alert('Update Failed');window.location='SystemPreference.aspx';";

                string qry = "UPDATE " + dbName + ".dbo.mstConfig SET ConfigValue = @ConfigValue WHERE " +
                    "(ConfigGroup = @ConfigGroup) AND (ConfigName = @ConfigName)";

                List<object> lstUpdate = new List<object>();

                // System Config Group
                if (string.IsNullOrEmpty(cbLogMode.Value.ToString()) == false){
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "System";
                    _dctParams["@ConfigName"] = "LoginMode";
                    _dctParams["@ConfigValue"] = cbLogMode.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }

                if (string.IsNullOrEmpty(txtSvrName.Value.ToString()) == false) {
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "System";
                    _dctParams["@ConfigName"] = "ServerAddress";
                    _dctParams["@ConfigValue"] = txtSvrName.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }
                if (string.IsNullOrEmpty(txtSvrPort.Value.ToString()) == false){
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "System";
                    _dctParams["@ConfigName"] = "ServerPort";
                    _dctParams["@ConfigValue"] = txtSvrPort.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }

                // Email Config Group
                if (string.IsNullOrEmpty(txtSMTPHost.Value.ToString()) == false){
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "Email";
                    _dctParams["@ConfigName"] = "Host";
                    _dctParams["@ConfigValue"] = txtSMTPHost.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }
                if (string.IsNullOrEmpty(txtSMTPPort.Value.ToString()) == false){
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "Email";
                    _dctParams["@ConfigName"] = "Port";
                    _dctParams["@ConfigValue"] = txtSMTPPort.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }
                if (string.IsNullOrEmpty(txtSMTPUname.Value.ToString()) == false){
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "Email";
                    _dctParams["@ConfigName"] = "UserName";
                    _dctParams["@ConfigValue"] = txtSMTPUname.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }
                if (string.IsNullOrEmpty(txtSMTPPwd.Value.ToString()) == false){
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "Email";
                    _dctParams["@ConfigName"] = "Password";
                    _dctParams["@ConfigValue"] = txtSMTPPwd.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }
                if (string.IsNullOrEmpty(cbSMTPCred.Value.ToString()) == false){
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "Email";
                    _dctParams["@ConfigName"] = "DefaultCredentials";
                    _dctParams["@ConfigValue"] = cbSMTPCred.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }
                if (string.IsNullOrEmpty(cbSMTPSSL.Value.ToString()) == false){
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "Email";
                    _dctParams["@ConfigName"] = "EnableSSL";
                    _dctParams["@ConfigValue"] = cbSMTPSSL.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }
                if (string.IsNullOrEmpty(txtEmailFrom.Value.ToString()) == false){
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "Email";
                    _dctParams["@ConfigName"] = "FromEmail";
                    _dctParams["@ConfigValue"] = txtEmailFrom.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }
                if (string.IsNullOrEmpty(txtEmailName.Value.ToString()) == false){
                    Dictionary<string, object> _dctParams = new Dictionary<string, object>();
                    _dctParams["@ConfigGroup"] = "Email";
                    _dctParams["@ConfigName"] = "FromName";
                    _dctParams["@ConfigValue"] = txtEmailName.Value.ToString();

                    lstUpdate.Add(_dctParams);
                }

                int Result = 0;
                Dictionary<string, object> ExecQry = new Dictionary<string, object>();
                int CountList = lstUpdate.Count();

                if (CountList > 0) {
                    for (int idx = 0; idx < CountList; idx++){
                        Dictionary<string, object> dctParams = new Dictionary<string, object>();
                        dctParams = (Dictionary<string, object>)lstUpdate[idx];

                        ExecQry = DBUtilities.ExecQuery(qry, dctParams, DBUtilities.QueryMode.Write);
                        if ((int)ExecQry["Status"] > 0){
                            Result = 1;
                        }else{
                            Result = 0;
                            break;
                        }
                    }
                }

                if (Result == 1){
                    Response.Write("<script>" + echoSuccess + "</script>");
                }else{
                    Response.Write("<script>" + echoFail + "</script>");
                }
            }
        }

        private static Dictionary<string, object> LoadData() {
            Dictionary<string, object> result = new Dictionary<string, object>();

            string dbName = DBUtilities.GetDBName("devCon");
            string qry = "SELECT mstConfig.ConfigGroup, mstConfig.ConfigName, mstConfig.ConfigValue FROM " +
                "" + dbName + ".dbo.mstConfig";
            SqlConnection cn = new SqlConnection(DBUtilities.strConnection("devCon"));
            using (cn) {
                try {
                    cn.Open();
                    SqlCommand cmd = new SqlCommand(qry, cn);
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows) {
                        Dictionary<string, object> dctEmail = new Dictionary<string, object>();
                        Dictionary<string, object> dctSystem = new Dictionary<string, object>();
                        while (rdr.Read()) {
                            string cfgGroup = rdr.GetValue(0).ToString();
                            if (cfgGroup == "Email") {
                                dctEmail.Add(rdr.GetValue(1).ToString(), rdr.GetValue(2));
                            }else if (cfgGroup == "System") {
                                dctSystem.Add(rdr.GetValue(1).ToString(), rdr.GetValue(2));
                            }
                        }
                        result.Add("Email", dctEmail);
                        result.Add("System", dctSystem);
                    }
                }catch(Exception ex){
                    string errMsg = ex.Message;
                }finally{
                    if (cn.State == ConnectionState.Open) {
                        cn.Close();
                    }
                }
            }

            return result;
        }
    }
}
